import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
console.log(uni.getStorageSync("userInfo"))
let hasLogin = (uni.getStorageSync("userInfo") && uni.getStorageSync("userInfo").id) ? true : false;
let theme = uni.getStorageSync("theme") ? uni.getStorageSync("theme") : 'light';
let baseUrl = uni.getStorageSync("baseUrl") ? uni.getStorageSync("baseUrl") :"http://ucj.su8.in";
// let baseUrl = uni.getStorageSync("baseUrl") ? uni.getStorageSync("baseUrl") :"";
console.log("111", hasLogin)
let proTyle = uni.getStorageSync("proTyle") ? uni.getStorageSync("proTyle") :"3";
let zhangHList = uni.getStorageSync("zhangHList") ? uni.getStorageSync("zhangHList") :[];

const store = new Vuex.Store({
	state: {
		hasLogin: hasLogin,
		userInfo: uni.getStorageSync("userInfo") || {},

		baseUrl:baseUrl,
		// 当前主题
		theme: theme,  //  light  dark
		isShow404:false,
		currentCode:"20230501",
		pubType:"",
		useAddress:"",
		proTyle:proTyle, //1商户c端 2物权c端 3 App
		zhangHList:zhangHList,
		isTixing:true,
		jiaoyiData1:0,
		jiaoyiData2:0
	},
	getters: {
	  theme(state) {
	    return state[state.theme]
	  }
	},
	mutations: {
		setJiaoyi1(state,item){
			state.jiaoyiData1 = item
		},
		setJiaoyi2(state,item){
			state.jiaoyiData2 = item
		},
		setIsTixing(state,item){
	
			state.isTixing = item
		},
		setZhangHList(state,item){
			uni.clearStorage(zhangHList)
			state.zhangHList = []
		},
		setProTyle(state,item){
			state.proTyle = item;
			uni.setStorageSync("proTyle",item)
		},
		setUseAddress(state,item){
			state.useAddress = item;
		},
		setisShow404(state,type){
			state.baseUrl = type;
			Vue.prototype.$apiUrl = type;
			uni.setStorageSync("baseUrl",type)
		},
		updateZhuti(state,type){
			uni.setStorageSync("theme",type)
			state.theme = type;
			
		},
		clearLogin(state,item){
			state.zhangHList = [];
			uni.removeStorage({ //缓存用户登陆状态
				key: 'zhangHList'
			})
			
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({
				key: 'userInfo'
			})
		},
		login(state, provider) {
			console.log("change login")
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({ //缓存用户登陆状态
				key: 'userInfo',
				data: provider
			})
			//多账号切换前置
			let kongArr = [];
			state.zhangHList.map(itemL =>{
				itemL.selC = 0;
				kongArr.push(itemL)
			})
			let isHave = kongArr.findIndex(itemc=>{
				return provider.id == itemc.id
			})
			if(isHave > -1){
				
				let beiDel = kongArr.splice(isHave,1);
				beiDel[0].selC = 1;
				kongArr.unshift(beiDel[0])
				
			}else{
				provider.selC = 1;
				kongArr.unshift(provider)
			}
			state.zhangHList = kongArr;
			uni.setStorage({ //缓存用户登陆状态
				key: 'zhangHList',
				data: kongArr
			})
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({
				key: 'userInfo'
			})
		},
	},
	actions: {

	},

})

export default store
